package com.tuyrt.hi.library.util

import android.graphics.Color

/**
 * author: Administrator on 2021/8/4.
 * desc:
 */
object ColorUtil {

    //根据百分比 计算出 start -->end 之间的 中间色
    fun getCurrentColor(startColor: Int, endColor: Int, fraction: Float): Int {
        //color = a r g b
        //endColor(argb)-startColor（argb）=difference(argb)
        //startColor（argb) +difference(argb)*fraction = newColor

        val alphaStart = Color.alpha(startColor)
        val redStart = Color.red(startColor)
        val blueStart = Color.blue(startColor)
        val greenStart = Color.green(startColor)

        val alphaEnd = Color.alpha(endColor)
        val redEnd = Color.red(endColor)
        val blueEnd = Color.blue(endColor)
        val greenEnd = Color.green(endColor)

        val alphaDiff = alphaEnd - alphaStart
        val redDiff = redEnd - redStart
        val blueDiff = blueEnd - blueStart
        val greenDiff = greenEnd - greenStart

        val alphaCurrent = (alphaStart + alphaDiff * fraction).toInt()
        val redCurrent = (redStart + redDiff * fraction).toInt()
        val blueCurrent = (blueStart + blueDiff * fraction).toInt()
        val greenCurrent = (greenStart + greenDiff * fraction).toInt()

        return Color.argb(alphaCurrent, redCurrent, blueCurrent, greenCurrent)
    }
}